$(document).ready(function(){
	var contextPath = document.location.pathname;
	var index = contextPath.substr(1).indexOf("/");
	contextPath = contextPath.substr(1, index);
	var basePath = "/" + contextPath;
	var radios = document.getElementsByName("isUse");
	initProvince();//加载省份
	var validObject = $("#addressInfor").Validform({
		dataType:'json',
		tiptype:2,
		ajaxPost:true,
		
		callback:function(data){
			var result = data.addressList;
			var memberId = result[0].memberId;
			alert("memberId----------->"+memberId);
			window.location.assign(basePath+"/HRTWEB/manageAddress.do?memberId="+memberId);
		},
	
	});
	/**
	 * 保存/修改地址
	 */
	$("#optBtn").delegate('#saveBtn', 'click', function() {
		
		callValidform();//调用验证框架
	});
	
	/**
	 * 修改按钮事件
	 */

	$("#inner")
			.delegate(
					'#editBtn',
					'click',
					function() {
						initProvince();//初始化省份
						var memberId = $(this).attr("memberId");
						var addressId = $(this).attr("addressId");
						var editUrl = basePath+"/HRTWEB/address/edit.do";
						$.ajax({

							url : editUrl,
							type : "POST",
							dataType : 'json',
							data : {
								memberId : memberId,
								addressId:addressId
							},
							cache : false,

							success : function(data) {
								var address = data.address;
								loadAddress(address);
							},
							error : function(html) {
								alert("Load Data failly");
								refresh();
							}
						});

					});
	
	/**
	 * 设置默认地址
	 */
	$("#inner")
	.delegate('#setDefaultBtn','click',
			function() {
				var memberId = $(this).attr("memberId");
				var addressId = $(this).attr("addressId");
				alert("memberId---------"+memberId);
				alert("addressId---------"+addressId);
				var editUrl = basePath+"/HRTWEB/address/setDefaultAddress.do";
				$.ajax({
					
					url : editUrl,
					type : "POST",
					dataType : 'json',
					data : {
						memberId : memberId,
						addressId:addressId
					},
					cache : false,
					
					success : function(data) {
						var address = data.address;//返回默认地址
						var memberId = address.memberId;
						alert("memberId----------->"+memberId);
						window.location.assign(basePath+"/HRTWEB/manageAddress.do?memberId="+memberId);
					},
					error : function(html) {
						alert("Load Data failly");
						refresh();
					}
				});
				
			});
	
	/**
	 * 模态窗口关闭时调用
	 */
	$("#addressBookInfor").on('hide.bs.modal', function() {
		clearAddress();
	});
	
	/**
	 * 地址
	 */
	 $(".inner").delegate(
			  "div[class='innerDIV']",
			  "click",
			  function(){
				 var addressDIV =  $(this).attr("id");
				 var addressId =  $(this).attr("objId");
				 alert("addressDIV-------->"+addressDIV);
				 alert("addressId-------->"+addressId);
				 var queryUrl =basePath+"/HRTWEB/address/getAddress.do?id="+addressId;
	            	$.ajax({
						url : queryUrl,
						dataType : 'json',
						cache : false,
						success : function(data) {
							var address = data.addressBook;
							/*alert(address.province);
							alert(address.postCode);
							alert(address.receiver);*/
							$("#receiver").attr("value",address.receiver);
							$("#postCode").attr("value",address.postCode);
							$("#province").attr("value",address.province);
							var tempAddress = address.province + address.city +address.area;
							$("#currentAddress").text(tempAddress);
							$("#receAddr").val(tempAddress+address.address);
							$("#currentReceiver").text(address.receiver);
						},
						error : function(html) {
							alert("出错了");
							return;
						}
					});
					
				 $("#"+addressDIV).attr("isselected","selected");
				 $("#"+addressDIV).css("border","1px red solid");
				 var divTotal = $("div[class='innerDIV']");
				 
				 var divObject = $("div[isselected='false']");
				 
				
				 
				 for(i=0;i<divObject.length;i++){
					var temp = $(divObject[i]).attr("id");
					$("#"+temp).css("border","");
				 }
				 
				 for(i=0;i<divTotal.length;i++){
						var temp = $(divTotal[i]).attr("id");
						$("#"+temp).attr("isselected","false");
						
				 }
			  });
			   

	 /**
	  * 初始化省份
	  */
	 function initProvince(){
		 var seachprov = $("#seachprov");
			var editUrl = basePath+"/HRTWEB/address/province/list.do";
			$.ajax({

				url : editUrl,
				dataType : 'json',
				cache : false,

				success : function(data) {
					var results = data.provinceList;
					
					var rowContent = "<option  value ='-1' >请选择</option>";
					if(results!=null){
						
						for(i=0;i<results.length;i++){
							var province =results[i];
							rowContent = rowContent+"<option  value ="+province.code+" >"+province.name+"</option>";
						}
					}
					seachprov.append(rowContent);
				},
				error : function(html) {
					alert("Load Data failly");
					refresh();
				}
			});
	 }
	 /**
	  * 验证方法
	  */
	 function callValidform(){
			
		 $("#addressInfor").attr("method", "post");

			var addressId = $("#addressId").val();
			alert("addressId---------->"+addressId);

			if (addressId == "") {
				$("#addressInfor").attr("action", ""+basePath+"/HRTWEB/address/save.do");
			} else {

				$("#addressInfor").attr("action", ""+basePath+"/HRTWEB/address/update.do");
			}
			
			//$("#addressInfor").Validform();
				
				
				
				
	}
});
function changePostType(cost){
	$("#postCost").text(cost);
	var summery = $("#tempSummery").text();
	var comCost = parseFloat(summery)+parseFloat(cost)
	$("#storeCost").text(comCost);
	$("#realCost").text(comCost);
	$("#summery").val(comCost);
	$("#mailFeed").val(cost);//邮费
}


function loadAddress(address) {
	$("#memberId").val(address.memberId);
	$("#addressId").val(address.id);
	$("#address").val(address.address);
	$("#postCode").val(address.postCode);
	$("#receiver").val(address.receiver);
	$("#mobile").val(address.mobile);
	$("#telPhone").val(address.telPhone);
	$("#addressBookInfor").modal('show');
}
function clearTable() {
	$("#addressBookTable tr:not(:first)").remove();
}
function clearAddress() {
	$("#seachprov").empty(); 
	$("#seachcity").empty(); 
	$("#seachdistrict").empty(); 
}
function refresh() {
	window.location.reload();
}





